#!/bin/bash
if [[ $(whoami) != "lfs" ]]; then
	echo "Please log in as user lfs...";
	exit 1;
fi
cd $LFS/sources/pkg || exit 1
source ./utils.sh || exit 2
#######################################
# set package name here
PACKAGE=$(echo $0 | sed -e 's/.*make_\(.*\).sh/\1/');
#######################################
PKG_NAME=$(pkgname basic $PACKAGE); if (($? != 0)); then exit 1; fi;
PKG_VERSION=$(pkgversion basic $PACKAGE); if (($? != 0)); then exit 2; fi;
PKG_ARCHIVE=$(pkgarchive basic $PACKAGE); if (($? != 0)); then exit 3; fi;
echo "make $PACKAGE ($PKG_NAME) with version $PKG_VERSION from archive $PKG_ARCHIVE"
PKG_FOLDER=$(unpack $PKG_ARCHIVE); if (($? != 0)); then exit 4; fi;
PKG_DEST=/usr/pkg/$PKG_NAME/$PKG_VERSION;
cd ../$PKG_FOLDER || exit 5;
#######################################
# prepare package source (patch etc.)
mkdir -v build && \
cd build




#######################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package preparing with code $ERR";
    exit 6;
fi
#######################################
# configure and make package
LIBS=-L/tools/lib                    \
CFLAGS=-I/tools/include              \
PKG_CONFIG_PATH=/tools/lib/pkgconfig \
../configure --prefix=/usr           \
             --bindir=/bin           \
             --with-root-prefix=""   \
             --enable-elf-shlibs     \
             --disable-libblkid      \
             --disable-libuuid       \
             --disable-uuidd         \
             --disable-fsck \
    && \
    make && \
    ln -sfv /tools/lib/lib{blk,uu}id.so.1 lib && \
    { make LD_LIBRARY_PATH=/tools/lib check | tee make_check.log; } || {
        errcho "$(cat make_check.log | tail -20)";
        read -p "Some tests fail, go on [y]? " ANSWER && [[ $ANSWER == "y"* ]] || exit 10;
    } && \
	rm -vRf $PKG_DEST/inst && \
    make DESTDIR=$PKG_DEST/inst install && \
    make DESTDIR=$PKG_DEST/inst install-libs





#######################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package building with code $ERR";
    exit 6;
fi
#######################################
# make post checks and configuration
chmod -v u+w $PKG_DEST/inst/usr/lib/{libcom_err,libe2p,libext2fs,libss}.a && \
gunzip -v $PKG_DEST/inst/usr/share/info/libext2fs.info.gz && \
install-info --dir-file=$PKG_DEST/inst/usr/share/info/dir $PKG_DEST/inst/usr/share/info/libext2fs.info && \
makeinfo -o      doc/com_err.info ../lib/et/com_err.texinfo && \
install -v -m644 doc/com_err.info $PKG_DEST/inst/usr/share/info && \
install-info --dir-file=$PKG_DEST/inst/usr/share/info/dir $PKG_DEST/inst/usr/share/info/com_err.info




#######################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package building with code $ERR";
    exit 7;
fi
echo "# Package information file... generated at make process" > $PKG_DEST/pkg.info && \
echo "# pkg name   source archive    version" >> $PKG_DEST/pkg.info && \
echo "$PKG_NAME  $PKG_ARCHIVE  $PKG_VERSION" >> $PKG_DEST/pkg.info && \
if [ -d $LFS/sources/pkg/basic/$PKG_NAME ]; then cp -aRv $LFS/sources/pkg/basic/$PKG_NAME/* $PKG_DEST; fi && \
cp -avf $LFS/sources/pkg/pkgutils.sh $PKG_DEST/pkgutils.sh && \
tar -czvvf /usr/pkg/${PKG_NAME}_${PKG_VERSION}_pkg.tar.gz -C /usr/pkg/$PKG_NAME $PKG_VERSION && \
rm -Rfv /usr/pkg/$PKG_NAME && \
cd $LFS/sources && \
rm -Rfv $PKG_FOLDER || exit 8;
echo "Build complete basic/$PACKAGE"
echo "Package files are stored to: /usr/pkg/${PKG_NAME}_${PKG_VERSION}_pkg.tar.gz"
echo " --OK--"
